<?php
// +----------------------------------------------------------------------
// | 文章分类控制器
// +----------------------------------------------------------------------
// | Author: Eric <ieyangxiaoguo@126.com>
// +----------------------------------------------------------------------
// | Copyright ©2017 http://www.zzuyxg.top All rights reserved.
// +----------------------------------------------------------------------
// | 2017年10月10日
// +----------------------------------------------------------------------
namespace app\tdadmin\controller;

use app\common\controller\AdminBaseController;
use app\common\model\CollegeModel;
use think\Db;
use think\Validate;

class CollegeController extends AdminBaseController
{
    /**
     * 学院列表
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 分类列表--数据表格数据接口
     * @author Eric ieyangxiaoguo@126.com
     * @param int $trashed 默认0，过滤已删除的分类；所有分类
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public function apiGetColleges($trashed=0)
    {
       $model = new CollegeModel();

       $categorys = $model->alias('a')->all();
        $res['code'] = 0;
        $res['msg'] = '';
        $res['data'] = $categorys;

        return $res;
    }

    /**
     * 添加分类
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $rules =   [
                'college_name'  => 'unique:College,college_name',
            ];
            $messages  =   [
                'college_name.unique' => '学院已存在',
            ];
            $validate = new Validate($rules,$messages);

            if ($validate->check($data)){
                $res['code'] = 1;
                $res['msg'] = '添加成功！';
                $model = new CollegeModel();
                $result = $model->addCollege($data);
                if ($result === false) {
                    $res['code'] = 0;
                    $res['msg'] = '添加失败！';
                }
                addLog("添加了学院：".$data['college_name'],Db::getLastSql());

                return $res;
            }else{
                $res['code'] = 0;
                $res['msg'] = $validate->getError();
                return $res;
            }
        } else {
            $categories = $this->apiGetColleges(1);
            $this->assign('categories', $categories['data']);
            return $this->fetch();
        }
    }

    /**
     * 编辑分类
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

                $check = CollegeModel::where('college_name',$data['college_name'])->find();
                if (isset($check) && $check['id'] != $data['id']){
                    $res['code'] = 0;
                    $res['msg'] = '学院名称已存在';
                    return $res;
                }
                $res['code'] = 1;
                $res['msg'] = '保存成功！';
                $result = CollegeModel::where('id',$data['id'])->update($data);
                if ($result === false) {
                    $res['code'] = 0;
                    $res['msg'] = '保存失败！';
                }
                addLog("修改了学院：".$data['college_name'],Db::getLastSql());
                return $res;

        } else {
            $id = $this->request->param('id', 0, 'intval');
            if ($id > 0) {
                $college = CollegeModel::get($id);
                $this->assign('college',$college);
                return $this->fetch();
            } else {
                $res['code'] = 0;
                $res['msg'] = '你从哪里来？';
                return $res;
            }
        }
    }

    //检测分类是否重复
    public function checkName()
    {
        $name = $this->request->param('name');

        $data['code'] = 1;
        $data['msg'] = '该名称可用！';

        if (CollegeModel::where('college_name',$name)->find()) {
            $data['code'] = 0;
            $data['msg'] = '该分类已存在';
        }

        return $data;
    }

    /**
     * 显示/开启操作
     *
     * @author Eric ieyangxiaoguo@126.com
     *         @date 2017年11月26日
     */
    public function doSwitch()
    {
        return parent::switchOperate(new CollegeModel());
    }

    /**
     * 更新字段
     */
    public function updateField()
    {
        return parent::fieldUpdate(new CollegeModel());
    }

    /**
     * 删除学院
     */
    public function delete()
    {
        $id = $this->request->param('id');
        if ($id) {
            $findCategory = CollegeModel::get($id);

            $result = $findCategory->delete(true);

            $msg = '删除';
            if ($result) {
                $data['code'] = 1;
                $data['msg'] = $msg.'成功！';
            } else {
                $data['code'] = 0;
                $data['msg'] = $msg.'失败！';
            }
        }else {
            $data['code'] = 0;
            $data['msg'] = '你从哪里来？';
        }

        return $data;
    }

    /**
     * 恢复分类
     */
    public function restore()
    {
        $id = $this->request->param('id');
        if ($id) {
            $categoryModel = new CategoryModel();
            // 获取删除的内容
            $findCategory = $categoryModel->onlyTrashed()->get($id);
            $result = $findCategory->restore();

            if ($result) {
                $data['code'] = 1;
                $data['msg'] = '恢复成功！';
            } else {
                $data['code'] = 0;
                $data['msg'] = '恢复失败！';
            }
        }else {
            $data['code'] = 0;
            $data['msg'] = '你从哪里开？';
        }

        return $data;
    }

}
